package com.neasho.neashoadminv1.web.console.sys;

import com.neasho.neashoadminv1.infrastructure.domain.R
import com.neasho.neashoadminv1.infrastructure.dto.sys.*
import com.neasho.neashoadminv1.infrastructure.permission.ApiAuth
import com.neasho.neashoadminv1.web.service.sys.SysMenuService
import jakarta.validation.Valid
import org.babyfish.jimmer.spring.model.SortUtils
import org.slf4j.LoggerFactory
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.web.bind.annotation.*

/**
 * 系统 - 菜单维护
 */
@RestController
@RequestMapping("/console/sys/menu")
class SysMenuController(
    val sysMenuService: SysMenuService
) {
    private val log = LoggerFactory.getLogger(SysMenuController::class.java)

    /**
     * 新增菜单
     */
    @PostMapping("/save")
    @ApiAuth([ "sys:menu:save" ])
    fun save(@Valid @RequestBody input: SysMenuAddInput): R<Int> {
        return R.affect(sysMenuService.save(input).totalAffectedRowCount)
    }

    /**
     * 修改菜单
     */
    @PutMapping("/update")
    @ApiAuth([ "sys:menu:update" ])
    fun update(@Valid @RequestBody input: SysMenuAddInput): R<Int> {
        return R.affect(sysMenuService.update(input).totalAffectedRowCount)
    }

    /**
     * 菜单分页查询
     */
    @GetMapping("/page")
    @ApiAuth([ "sys:menu:page" ])
    fun page(@Valid spec: SysMenuPageSpec): R<Page<SysMenuPageView>> {
        return R.ok(
            sysMenuService.page(
                spec, SysMenuPageView::class, PageRequest.of(
                    spec.pageNumber - 1, spec.pageSize, SortUtils.toSort("id desc")
                )
            )
        )
    }

    /**
     * 查询列表
     */
    @GetMapping("/list")
    @ApiAuth([ "sys:menu:list" ])
    fun list(@Valid spec: SysMenuListSpec): R<List<SysMenuListView>> {
        return R.ok(sysMenuService.list(spec, SysMenuListView::class))
    }

    /**
     * 查询树结构
     */
    @GetMapping("/tree")
    @ApiAuth([ "sys:menu:tree" ])
    fun tree(@Valid spec: SysMenuListSpec): R<List<SysMenuTreeView>> {
        return R.ok(sysMenuService.tree(spec, SysMenuTreeView::class))
    }

    /**
     * 查询菜单详情
     */
    @GetMapping("/detail")
    @ApiAuth([ "sys:menu:detail" ])
    fun detail(@Valid spec: SysMenuDetailSpec): R<SysMenuDetailView> {
        return R.ok(sysMenuService.findById(spec, SysMenuDetailView::class))
    }

    /**
     * 逻辑删除菜单
     */
    @DeleteMapping("/delete")
    @ApiAuth([ "sys:menu:delete" ])
    fun delete(@Valid spec: SysMenuLogicDeleteInput): R<Int> {
        return R.affect(sysMenuService.deleteByPhysical(spec.id).totalAffectedRowCount)
    }

    /**
     * 批量删除菜单
     */
    @DeleteMapping("/batch-delete-by-ids")
    @ApiAuth([ "sys:menu:batch-delete-by-ids" ])
    fun batchDeleteByIds(@Valid spec: SysMenuBatchDeleteInput): R<Int> {
        return R.affect(sysMenuService.batchDeleteByIds(spec.ids).totalAffectedRowCount)
    }

    /**
     * 批量修改
     */
    @PutMapping("/batch-update")
    @ApiAuth([ "sys:menu:batch-update" ])
    fun batchUpdate(@Valid @RequestBody list: List<SysMenuUpdateInput>): R<Int> {
        return R.affect(sysMenuService.batchUpdate(list))
    }

    /**
     * 批量新增
     */
    @PostMapping("/batch-save")
    @ApiAuth([ "sys:menu:batch-save" ])
    fun batchSave(@Valid @RequestBody list: List<SysMenuAddInput>): R<Int> {
        return R.affect(sysMenuService.batchSave(list))
    }

    /**
     * 导入模板
     */
    @GetMapping("/import-template")
    @ApiAuth([ "sys:menu:import-template" ])
    fun importTemplate(): R<String> {
        TODO("not yet implemented")
    }

    /**
     * 导出数据
     */
    @GetMapping("/export-data")
    @ApiAuth([ "sys:menu:export-data" ])
    fun export(): R<String> {
        TODO("not yet implemented")
    }

    /**
     * 导入数据
     */
    @PostMapping("/import-data")
    @ApiAuth([ "sys:menu:import-data" ])
    fun importData(): R<String> {
        TODO("not yet implemented")
    }

}
